const mongoose = require('mongoose');

mongoose.connect('mongodb://zhaouser:6eAfJUwp6WfuQKS6@175.178.67.243:27017/zhaohuishou'); 
// https://www.cnblogs.com/yangzhou33/p/8996316.html

const { Schema, model } = mongoose

// 微信用户
class WxUser {
  constructor() {
    this.wxUserSchema = Schema({
      openid: String,
      nickName: String,
      avatarUrl: String,
      integral: Number,
      studentId: String,
      name: String
    })
    this.wxUserModel = model('wxUsers', this.wxUserSchema)
  }

  findWxUser(param) {
    return this.wxUserModel.find(param)
  }
  saveWxUser(param) {
    return new this.wxUserModel(param).save()
  }
  updateWxUser(fillter,update) {
    return this.wxUserModel.updateOne(fillter,update)
  } 
  deleteWxUser(param) {
    return this.wxUserModel.deleteMany(param)
  }
}

// 后台用户
class User {
  constructor() {
    this.userSchema = Schema({
      name: String,
      password: String
    })
    this.userModel = model('users', this.userSchema)
  }

  findUser(param) {
    return this.userModel.find(param)
  }
  saveUser(param) {
    return new this.userModel(param).save()
  }
}

// 预约
class Subscribe {
  constructor() {
    this.subscribeSchema = Schema({
      userId: String,
      nickName: String,
      ordertime: String,
      dormitoryArea: String,
      dormitoryNumber: String,
      phone: String
    })
    this.subscribeModel = model('subscribes', this.subscribeSchema)
  }

  findSubscribe(param) {
    return this.subscribeModel.find(param)
  }
  saveSubscribe(param) {
    return new this.subscribeModel(param).save()
  }
  deleteSubscribe(param) {
    return this.subscribeModel.deleteMany(param)
  }
}
 
// 活动
class Activity {
  constructor() {
    this.activitySchema = Schema({
      userId: String,
      nickName: String,
      activityName: String,
      activityTime: String,
      activityTarget: String,
      activityContent: String,
      img1: String,
      img2: String,
      img3: String,
      status: Number,
    })
    this.activityModel = model('activities', this.activitySchema)
  }

  findActivity(param) {
    return this.activityModel.find(param)
  }
  saveActivity(param) {
    return new this.activityModel(param).save()
  }
  updateActivity(fillter,update) {
    return this.activityModel.updateOne(fillter,update)
  }
  deleteActivity(param) {
    return this.activityModel.deleteMany(param)
  }
}

// 报名参加活动
class UserAndActivity  {
  constructor() {
    this.userAndActivitySchema = Schema({
      userId: String,
      activityId: String,
      time: String
    })
    this.userAndActivityModel = model('userAndActivity', this.userAndActivitySchema)
  }

  findUserAndActivity(param) {
    return this.userAndActivityModel.find(param)
  }
  saveUserAndActivity(param) {
    return new this.userAndActivityModel(param).save()
  }
  deleteUserAndActivity(param) {
    return this.userAndActivityModel.deleteMany(param)
  }
}

// 商品
class Goods {
  constructor() {
    this.goodsSchema = Schema({
      name: String,
      price: Number,
      img: String,
    })
    this.goodsModel = model('goods', this.goodsSchema)
  }

  findGoods(param) {
    return this.goodsModel.find(param)
  }
  saveGoods(param) {
    return new this.goodsModel(param).save()
  }
  deleteGoods(param) {
    return this.goodsModel.findOneAndDelete(param)
  }
}

// 意见
class Opinion {
  constructor() {
    this.opinionSchema = Schema({
      userId: String,
      nickName: String,
      time: String,
      content: String
    })
    this.opinionModel = model('opinions', this.opinionSchema)
  }

  findOpinion(param) {
    return this.opinionModel.find(param)
  }
  saveOpinion(param) {
    return new this.opinionModel(param).save()
  }
  deleteOpinion(param) {
    return this.opinionModel.deleteMany(param)
  }
}

// 轮播图

class Graph {
  constructor() {
    this.graphSchema = Schema({
      ordinal: Number,
      img: String,
    })
    this.graphModel = model('graphes', this.graphSchema)
  }

  findGraph(param) {
    return this.graphModel.find(param)
  }
  saveGraph(param) {
    return new this.graphModel(param).save()
  }
  updateGraph(fillter,update) {
    return this.graphModel.updateOne(fillter,update)
  }
  deleteGraph(param) {
    return this.graphModel.deleteMany(param)
  }
}

// 积分
class Integral {
  constructor() {
    this.integralSchema = Schema({
      userId: String,
      nickName: String,
      type: String,
      message: String,
      record: String,
      time: String,
    })
    this.integralModel = model('integral', this.integralSchema)
  }

  findIntegral(param) {
    return this.integralModel.find(param)
  }
  saveIntegral(param) {
    return new this.integralModel(param).save()
  }
  updateIntegral(fillter,update) {
    return this.integralModel.updateOne(fillter,update)
  }
  deleteIntegral(param) {
    return this.integralModel.deleteMany(param)
  }
}

// 分类
class Classify {
  constructor() {
    this.classifySchema = Schema({
      name: String,
      type: String,
      time: String,
    })
    this.classifyModel = model('classifies', this.classifySchema)
  }

  findClassify(param) {
    return this.classifyModel.find(param)
  }
  saveClassify(param) {
    return new this.classifyModel(param).save()
  }
  updateClassify(fillter,update) {
    return this.classifyModel.updateOne(fillter,update)
  }
  deleteClassify(param) {
    return this.classifyModel.deleteMany(param)
  }
}

class Scan {
  constructor() {
    this.scanSchema = Schema({
      userId: String,
      nickName: String,
      message: String,
      time: String,
    })
    this.scanModel = model('sacn', this.scanSchema)
  }

  findScan(param) {
    return this.scanModel.find(param)
  }
  saveScan(param) {
    return new this.scanModel(param).save()
  }
  updateScan(fillter,update) {
    return this.scanModel.updateOne(fillter,update)
  }
  deleteScan(param) {
    return this.scanModel.deleteMany(param)
  }
}

module.exports = {
  wxUser: new WxUser(),
  user: new User(),
  subscribe: new Subscribe(),
  activity: new Activity(),
  userAndActivity: new UserAndActivity(),
  goods: new Goods(),
  opinion: new Opinion(),
  graph: new Graph(),
  integral: new Integral(),
  classify: new Classify(),
  scan: new Scan(),
}